package com.hopoke.common.advice;

import com.hopoke.common.domain.Result;
import com.hopoke.common.domain.ResultCodeEnum;
import com.hopoke.common.expection.CommonException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class CommonExceptionAdvice {

    @ExceptionHandler(CommonException.class)
    public Result<Void> handleCommonException(CommonException e) {
        log.error("自定义异常 -> {} , 异常原因：{}  ",e.getClass().getName(), e.getMessage());
        log.debug("", e);
//        e.printStackTrace();
        return Result.error(e);
    }


    @ExceptionHandler(RuntimeException.class)
    public Object handleRuntimeException(RuntimeException e) {
        log.error("其他异常 uri : {} -> ", "", e);
//        e.printStackTrace();
        return  Result.error(new CommonException(e, ResultCodeEnum.SERVER_ERROR));
    }

}
